package net.goutalk.glcs.module.print.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import net.goutalk.glcs.module.form.entity.FormTemplate;
import net.goutalk.glcs.module.form.mapper.FormTemplateMapper;
import net.goutalk.glcs.module.print.dto.PrintConfigInfoDto;
import net.goutalk.glcs.module.print.dto.PrintSetConfigDto;
import net.goutalk.glcs.module.print.entity.PrintConfig;
import net.goutalk.glcs.module.print.entity.PrintSchema;
import net.goutalk.glcs.module.print.mapper.PrintConfigMapper;
import net.goutalk.glcs.module.print.mapper.PrintSchemaMapper;
import net.goutalk.glcs.module.print.service.IPrintConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.goutalk.glcs.module.print.vo.PrintConfigInfoVo;
import net.goutalk.glcs.module.system.entity.Menu;
import net.goutalk.glcs.module.system.mapper.MenuMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author tanyujie
 * @since 2023-05-23
 */
@Service
@AllArgsConstructor
public class PrintConfigServiceImpl extends ServiceImpl<PrintConfigMapper, PrintConfig> implements IPrintConfigService {

    private final MenuMapper menuMapper;

    private final PrintSchemaMapper printSchemaMapper;

    private final FormTemplateMapper formTemplateMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean setConfig(PrintSetConfigDto dto) {

        remove(Wrappers.lambdaQuery(PrintConfig.class).eq(PrintConfig::getMenuId, dto.getMenuId()).eq(PrintConfig::getButtonCode, dto.getButtonCode()));

        PrintConfig printConfig = BeanUtil.toBean(dto, PrintConfig.class);
        save(printConfig);
        return Boolean.TRUE;
    }

    @Override
    public PrintConfigInfoVo getInfo(PrintConfigInfoDto dto) {
        PrintConfigInfoVo printConfigInfoVo = new PrintConfigInfoVo();
        PrintConfig config = getOne(Wrappers.lambdaQuery(PrintConfig.class).eq(PrintConfig::getMenuId, dto.getMenuId()).eq(PrintConfig::getButtonCode, dto.getButtonCode()));
        if (config != null) {
            Long schemaId = config.getSchemaId();
            PrintSchema schema = printSchemaMapper.selectOne(Wrappers.lambdaQuery(PrintSchema.class).eq(PrintSchema::getId,schemaId).select(PrintSchema::getEnabledMark));

            if (ObjectUtil.isNotNull(config)) {
                printConfigInfoVo = BeanUtil.toBean(config, PrintConfigInfoVo.class);
            }

            if (ObjectUtil.isNotNull(schema)) {
                printConfigInfoVo.setEnabledMark(schema.getEnabledMark());
            }
        }
        Menu menu = menuMapper.selectById(dto.getMenuId());

        if (ObjectUtil.isNotNull(menu.getFormId())) {
            FormTemplate formTemplate = formTemplateMapper.selectById(menu.getFormId());
            if (ObjectUtil.isNotNull(formTemplate)) {
                printConfigInfoVo.setFormJson(formTemplate.getFormJson());
                printConfigInfoVo.setFormType(formTemplate.getFormType());
            }
        }

        return printConfigInfoVo;
    }
}
